package views;

import DAO.CursoDao;
import DAO.GradoDao;
import views.panels.PanelGeneral;
import java.awt.BorderLayout;
import java.util.Iterator;
import java.util.List;
import models.Curso;
import models.Estudiante;
import models.Grado;

/**
 *
 * @author vanessa
 */
public class InterfazConsultarCurso extends javax.swing.JFrame {

    /** Creates new form InterfazConsultarCurso */
    public InterfazConsultarCurso() {
        initComponents();
        PanelGeneral panel = new PanelGeneral();
        this.add(panel, BorderLayout.SOUTH);
        inicializarComboBox();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        CBGrado = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        CBCurso = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        CPListado = new javax.swing.JScrollPane();
        TListEstudiantes = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        CTCurso = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        CTDirectorCurso = new javax.swing.JTextField();
        BAceptar = new javax.swing.JButton();

        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setForeground(new java.awt.Color(0, 0, 255));
        jLabel1.setText("SELECCIONE EL CURSO:");

        jLabel2.setText("Grado:");

        CBGrado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione" }));
        CBGrado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBGradoActionPerformed(evt);
            }
        });

        jLabel3.setText("Curso:");

        CBCurso.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione" }));
        CBCurso.setEnabled(false);
        CBCurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBCursoActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("Listado de estudiantes inscritos en el curso:");

        TListEstudiantes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Documento de Identificación", "Nombre del Estudiante"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        CPListado.setViewportView(TListEstudiantes);

        jLabel5.setText("Curso");

        jLabel6.setText("Director de Curso:");

        BAceptar.setFont(new java.awt.Font("Tahoma", 1, 11));
        BAceptar.setText("Aceptar");
        BAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BAceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(CPListado, javax.swing.GroupLayout.DEFAULT_SIZE, 584, Short.MAX_VALUE)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(CBGrado, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(CBCurso, 0, 338, Short.MAX_VALUE))
                            .addComponent(jLabel4)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(CTCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(CTDirectorCurso, javax.swing.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(164, 164, 164)
                        .addComponent(BAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(CBGrado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(CBCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(CPListado, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(CTCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(CTDirectorCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(BAceptar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BAceptarActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_BAceptarActionPerformed

    private void CBGradoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBGradoActionPerformed
        if (CBGrado.getSelectedIndex() > 0){
            if (CBGrado.getSelectedIndex() > 0){
                GradoDao gradoDao = new GradoDao();
                Grado grado = gradoDao.encontrarGrado(Integer.parseInt((String)CBGrado.getSelectedItem()));
                CBCurso.setEnabled(true);
                CursoDao cursoDao = new CursoDao();
                List cursos = cursoDao.encontrarCursosGrado(grado.getIdGrado());
                if (cursos.size() > 0){
                    CBCurso.setEnabled(true);
                    for (Iterator it = cursos.iterator(); it.hasNext();) {
                        Curso curso = (Curso) it.next();
                        CBCurso.addItem(curso.getNombrecurso());
                    }
                }
            }
            CBCurso.setSelectedIndex(0);
        }
        else{
            CBCurso.setEnabled(false);
        }
    }//GEN-LAST:event_CBGradoActionPerformed

    private void CBCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBCursoActionPerformed
        CursoDao cursoDao = new CursoDao();
        Curso curso = cursoDao.encontrarCursoPorNombre((String)CBCurso.getSelectedItem());
        List estudiantes = curso.getEstudianteList();
        int fila = 0;
        for (Iterator it = estudiantes.iterator(); it.hasNext();) {
            Estudiante estudiante = (Estudiante) it.next();
            TListEstudiantes.setValueAt(estudiante.getIdEstudiante(), fila, 0);
            TListEstudiantes.setValueAt(estudiante.getNombreest(), fila, 1);
        }
        while (TListEstudiantes.getValueAt(fila, 0) != null){
            TListEstudiantes.setValueAt(null, fila, 0);
            TListEstudiantes.setValueAt(null, fila, 1);
        }
        CTDirectorCurso.setText(curso.getProfesorId().getNombresprofesor());
        CTCurso.setText(curso.getNombrecurso());
    }//GEN-LAST:event_CBCursoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InterfazConsultarCurso().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BAceptar;
    private javax.swing.JComboBox CBCurso;
    private javax.swing.JComboBox CBGrado;
    private javax.swing.JScrollPane CPListado;
    private javax.swing.JTextField CTCurso;
    private javax.swing.JTextField CTDirectorCurso;
    private javax.swing.JTable TListEstudiantes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    // End of variables declaration//GEN-END:variables

    private void inicializarComboBox() {
        GradoDao gradoDao = new GradoDao();
        List grados = gradoDao.encontrarGrados();
        for (Iterator it = grados.iterator(); it.hasNext();) {
            Grado grado = (Grado) it.next();
            CBGrado.addItem(String.valueOf(grado.getIdGrado()));
        }
    }
}
